1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: PropertyHelper.java,v 1.2 2004/02/03 07:31:03 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.message.properties;
46
47 import java.util.Enumeration;
48 import java.util.HashMap;
49 import java.util.HashSet;
50 import java.util.Map;
51 import java.util.Set;
52
53 import javax.jms.JMSException;
54 import javax.jms.Message;
55
56 import org.exolab.jmscts.core.ClassHelper;
57 import org.exolab.jmscts.test.message.util.PropertyValues;
58
59
60 /***
61 * Helper class providing methods to populate messages with properties, and
62 * and return the properties in messages
63 *
64 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
65 * @version $Revision: 1.2 $
66 */
67 final class PropertyHelper implements PropertyValues {
68
69 /***
70 * Prevent construction of helper class
71 */
72 private PropertyHelper() {
73 }
74
75 /***
76 * Sets user properties for each property type
77 *
78 * @param message the message to set properties on
79 * @return the set of property names populated
80 * @throws Exception if any of the JMS operations fail
81 */
82 public static Set setProperties(Message message) throws Exception {
83 Set set = new HashSet();
84 int count = 0;
85 for (int i = 0; i < ALL_VALUES.length; ++i) {
86 setPrimitiveProperties(message, ALL_VALUES[i], set);
87 setObjectProperties(message, ALL_VALUES[i], set);
88 count += ALL_VALUES[i].length * 2;
89 }
90
91 // make sure all properties were set
92 if (set.size() != count) {
93 throw new Exception("Expected " + count + " properties to be "
94 + "set, but got " + set.size());
95 }
96 return set;
97 }
98
99 /***
100 * Helper method to set an array of java primitive properties on a
101 * message.
102 * The names assigned to each property is populated in the supplied set.
103 *
104 * @param message the message to set the properties on
105 * @param values the array of java primitive values
106 * @param set the set to populate with the property names
107 * @throws JMSException if a property cannot be set
108 */
109 public static void setPrimitiveProperties(
110 Message message, Object[] values, Set set) throws JMSException {
111
112 for (int i = 0; i < values.length; ++i) {
113 Object value = values[i];
114 String name = getName(ClassHelper.getPrimitiveType(
115 value.getClass()), i);
116 setPrimitiveProperty(message, name, value);
117 set.add(name);
118 }
119 }
120
121 /***
122 * Helper method to set an array of object properties on a message.
123 * The names assigned to each property is populated in the supplied set.
124 *
125 * @param message the message to set the properties on
126 * @param values the array of object values
127 * @param set the set to populate with the property names
128 * @throws JMSException if a property cannot be set
129 */
130 public static void setObjectProperties(Message message, Object[] values,
131 Set set) throws JMSException {
132
133 for (int i = 0; i < values.length; ++i) {
134 Object value = values[i];
135 String name = null;
136 if (value instanceof String) {
137 name = "java_lang_String" + i;
138 // strings and object properties are generally treated the
139 // same way, so prefix with the package name to avoid clashes
140 // with the setStringProperty() method
141 } else {
142 name = getName(value.getClass(), i);
143 }
144 message.setObjectProperty(name, value);
145 set.add(name);
146 }
147 }
148
149 /***
150 * Helper method to set a java primitive property on a message
151 *
152 * @param message the message to set the property on
153 * @param name the name of the property
154 * @param value the primitive value, wrapped in its corresponding Object
155 * type
156 * @throws JMSException if the property cannot be set
157 */
158 public static void setPrimitiveProperty(
159 Message message, String name, Object value) throws JMSException {
160
161 if (value instanceof Boolean) {
162 message.setBooleanProperty(name, ((Boolean) value).booleanValue());
163 } else if (value instanceof Byte) {
164 message.setByteProperty(name, ((Byte) value).byteValue());
165 } else if (value instanceof Short) {
166 message.setShortProperty(name, ((Short) value).shortValue());
167 } else if (value instanceof Integer) {
168 message.setIntProperty(name, ((Integer) value).intValue());
169 } else if (value instanceof Long) {
170 message.setLongProperty(name, ((Long) value).longValue());
171 } else if (value instanceof Float) {
172 message.setFloatProperty(name, ((Float) value).floatValue());
173 } else if (value instanceof Double) {
174 message.setDoubleProperty(name, ((Double) value).doubleValue());
175 } else {
176 message.setStringProperty(name, (String) value);
177 }
178 }
179
180 /***
181 * Returns all of the user properties of a message
182 *
183 * @param message the message to extract the properties from
184 * @return a map containing the user properties
185 * @throws JMSException if the JMS operations fail
186 */
187 public static Map getProperties(Message message) throws JMSException {
188 Map result = new HashMap();
189 Enumeration iter = message.getPropertyNames();
190 while (iter.hasMoreElements()) {
191 String name = (String) iter.nextElement();
192 Object value = message.getObjectProperty(name);
193 result.put(name, value);
194 }
195 return result;
196 }
197
198 /***
199 * Helper to returns a property identifier name based on its class name
200 * and a int value
201 *
202 * @param type the property class
203 * @param id a unique identifier for the particular property class
204 * @return the property identifier
205 */
206 private static String getName(Class type, int id) {
207 String name = null;
208 if (type.equals(String.class)) {
209 name = "String";
210 } else {
211 if (type.isPrimitive()) {
212 name = type.getName();
213 } else {
214 name = type.getName().replace('.', '_');
215 }
216 }
217 return name + id;
218 }
219
220 }
This page was automatically generated by Maven